package com.wedu.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wedu.common.utils.PageUtils;
import com.wedu.common.utils.Query;
import com.wedu.modules.sys.dao.GameDao;
import com.wedu.modules.sys.dao.SysDictDao;
import com.wedu.modules.sys.entity.GameEntity;

import com.wedu.modules.sys.entity.SysDictEntity;
import com.wedu.modules.sys.redis.GameRedis;
import com.wedu.modules.sys.service.GameService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service("gameService")
public class GameServiceImpl extends ServiceImpl<GameDao, GameEntity> implements GameService {
    @Autowired
    private GameRedis gameRedis;

    @Autowired
    private SysDictDao sysDictDao;

    /**
     * 分页查询
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String gameName = (String)params.get("gameName");
        String isAsc = (String) params.get("isAsc");  // 获取排序方式
        QueryWrapper<GameEntity> queryWrapper = new QueryWrapper<GameEntity>()
                .like(StringUtils.isNotBlank(gameName), "game_name", gameName)
                .eq("deleted", 0);

            // 判断排序方式，默认按升序排序
            if ("desc".equalsIgnoreCase(isAsc)) {
                queryWrapper.orderByDesc("launch_time");  // 降序排序
            } else {
                queryWrapper.orderByAsc("launch_time");  // 升序排序
            }

        IPage<GameEntity> page = this.page(
                new Query<GameEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    /**
     * 插入一条游戏数据
     * @param game
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveGame(GameEntity game) {
        this.save(game);
        gameRedis.saveOrUpdate(game);
    }

    /**
     * 修改游戏数据
     * @param game
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(GameEntity game) {
        this.updateById(game);
        gameRedis.saveOrUpdate(game);
    }

    /**
     * 逻辑删除游戏
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for(Long id : ids){
            GameEntity game = this.getById(id);
            gameRedis.delete(game.getGameName());
        }
        this.removeByIds(Arrays.asList(ids));
    }

    /**
     * 查询字典选项
     * @return
     */
    @Override
    public Map<String, Object> queryChildPage() {
        QueryWrapper<SysDictEntity>  gameTypeQueryWrapper=new QueryWrapper<>();
        gameTypeQueryWrapper.eq("code", "gameType")
                .orderByAsc("sort")
                .eq("type", "2");//查询游戏类型字典选项列表
        QueryWrapper<SysDictEntity>  ageGroupQueryWrapper=new QueryWrapper<>();
        ageGroupQueryWrapper.eq("code", "ageGroup")
                .orderByAsc("sort")
                .eq("type", "2");//查询年龄分级字典选项列表

       // List<SysDictEntity> dictEntityList = sysDictDao.selectList(dictEntityQueryWrapper);
        List<SysDictEntity> gameTypeList = sysDictDao.selectList(gameTypeQueryWrapper);
        List<SysDictEntity> ageGroupList = sysDictDao.selectList(ageGroupQueryWrapper);
        Map<String, Object> dictMap = new HashMap<>();
//        for (SysDictEntity dictEntity : dictEntityList) {
//
//            if (dictEntity.getCode().equals("gameType")){
//                gameTypeList.add(dictEntity);
//            } else if (dictEntity.getCode().equals("ageGroup")){
//                ageGroupList.add(dictEntity);
//            }
//
//        }

        dictMap.put("gameTypeList",gameTypeList);
        dictMap.put("ageGroupList",ageGroupList);
        return dictMap;

    }

}